/**
 *
 */
package de.cbf.cam.dsa.rcp.model.entities;

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

import de.cbf.util.uuidheader.entities.UUIDHeader;
import de.cbf.util.uuidheader.exceptions.EntityMergeException;

/**
 * @author Surfer
 *
 */
@XmlRootElement(name = "culture")
public class CultureDSA extends RCPPerked {

	/**
	 * if this is not null, than this is sub culture<br>
	 * SubCultures do not hold all data, but masterCulture does<br>
	 * so always check master culture if data is missing
	 * TODO find a place to get all subcultures(by iterating) for a masterCulture, would be provider/persister
	 */
	protected CultureDSA masterCulture;


	protected CultureNameData cultureNameData;


	public CultureDSA() {
		super();
	}


	@Override
	protected void doMergeDataFrom(UUIDHeader entity, boolean i_am_older)
			throws EntityMergeException {
		super.doMergeDataFrom(entity, i_am_older);
		if (entity instanceof CultureDSA) {
			CultureDSA correctEntity = (CultureDSA) entity;
			if(i_am_older){
				this.cultureNameData.mergeDataFrom(correctEntity.cultureNameData);
				this.masterCulture = correctEntity.masterCulture;
			}
		}else{
			String mergeEntityClass = entity == null? "null":entity.getClass().getName();
			String errorMsg ="EntityToMerge "+mergeEntityClass+" does not fit class:"+this.getClass().getName();
			throw new EntityMergeException(errorMsg);
		}
	}


	@Override
	public Object clone() throws CloneNotSupportedException {
		CultureDSA clone = new  CultureDSA();
		try {
			clone.mergeDataFrom(this);
		} catch (EntityMergeException e) {
			throw new CloneNotSupportedException(e.getLocalizedMessage());
		}
		return clone;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((cultureNameData == null) ? 0 : cultureNameData.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		CultureDSA other = (CultureDSA) obj;
		if (cultureNameData == null) {
			if (other.cultureNameData != null)
				return false;
		} else if (!cultureNameData.equals(other.cultureNameData))
			return false;
		return true;
	}

	public CultureNameData getCultureNameData() {
		if(cultureNameData != null && masterCulture != null){
			return masterCulture.getCultureNameData();
		}
		return cultureNameData;
	}

	public List<PerkDSA> getWeightedPerks() {
		if(weightedPerks == null && masterCulture != null){
			return masterCulture.getWeightedPerks();
		}
		return weightedPerks;
	}

	public void setCultureNameData(CultureNameData cultureNameData) {
		this.cultureNameData = cultureNameData;
	}


	public CultureDSA getMasterCulture() {
		return masterCulture;
	}


	public void setMasterCulture(CultureDSA masterCulture) {
		this.masterCulture = masterCulture;
	}





}
